function searchFileList(isOther, javCode, cls, callback, callFinish = null, callInfo = null) {
    //保存查询关键词参数
    //GM_setValue("115_search_var", `${javId}|${javId2}|${javId3}|${javId4}`);

    let url = null;
    if (cls === 'preview') {
        url = `http://localhost:10168/search?imgcode=${javCode}`;
    } else if (cls === 'longimg') {
        url = `http://localhost:10168/longsearch?imgcode=${javCode}`;
    }
    if (!url) return;

    // 使用 fetch 发送 GET 请求
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
                callback(-2, null);
            }
            // 解析JSON
            return response.json();
        })
        .then(resultJson => {
            // 处理JSON数据
            if (resultJson.length > 0) {

                for (let i = 0; i < resultJson.length; i++) {
                    let row = resultJson[i];

                    if (row) {
                        callback(0, row);
                    }
                }
                return true;

            }else {
                throw new Error('Element not found');
            }

        })
        .then(res => {
            if (callFinish) {
                setTimeout(function () {
                    callFinish(res);
                }, 500);
            }

        })
        .catch(error => {
            // 处理请求过程中出现的错误
            console.error('fetch error:', error);
            if (isOther && error.message === 'Network response was not ok') {
                searchFileList_javdb(javCode, cls, callback, callFinish, callInfo);

            } else if (isOther && error.message === 'Element not found') {
                searchFileList_javdb(javCode, cls, callback, callFinish, callInfo);

            } else {
                callback(-1, null);
            }
        });

}

function searchFileList_tor05(javCode, cls, callback, callFinish = null, callInfo = null) {
    // 请求URL
    // const url = 'http://localhost:10168/proxy?target_url=https://tor05.com/en/videos/' + javCode.toLowerCase();
    const url = 'http://localhost:10168/site/tor05/' + javCode.toLowerCase() + '.html';

    // 使用fetch API发送请求
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 将响应转换为文本（如果是HTML页面）
            return response.text();
        })
        .then(html => {
            // 创建一个DOM解析器
            const parser = new DOMParser();
            // 将HTML字符串解析为DOM文档
            const doc = parser.parseFromString(html, 'text/html');
            // 查询特定的元素
            const imgAaalist = doc.querySelectorAll('#preview-img .glightbox2');
            // 处理查询到的元素
            if (imgAaalist && imgAaalist.length > 0) {
                console.log('Found element:', imgAaalist);
                // 你可以在这里对elements进行进一步处理，比如获取其属性或内容
                for (let i = 0; i < imgAaalist.length; i++) {
                    let aaa = imgAaalist[i];
                    callback(2, aaa.href);
                }
                return true;

            } else {
                throw new Error('Element not found');
            }
        }).then(res => {
        if (callFinish) {
            setTimeout(function () {
                callFinish(res);
            }, 500);
        }

    }).catch(error => {
        // 处理错误
        console.error('fetch error:', error);
        if (error.message === 'Network response was not ok') {
            callback(-1, null);

        } else if (error.message === 'Element not found') {
            callback(-1, null);

        } else {
            callback(-1, null);
        }
    });
}

function searchFileList_mfinder(javCode, cls, callback, callFinish = null, callInfo = null) {
    // 请求URL
    // const url = 'http://localhost:10168/proxy?target_url=https://tor05.com/en/videos/' + javCode.toLowerCase();
    const url = 'http://localhost:10168/site/mfinder/' + javCode.toLowerCase() + '.html';

    // 使用fetch API发送请求
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 将响应转换为文本（如果是HTML页面）
            return response.text();
        })
        .then(html => {
            // 创建一个DOM解析器
            const parser = new DOMParser();
            // 将HTML字符串解析为DOM文档
            const doc = parser.parseFromString(html, 'text/html');

            const lableObj = {};
            let sss = doc.querySelector('.text-warning');
            if (sss){
                lableObj.lable_code = sss.textContent;
            }else {
                lableObj.lable_code = '';
            }

            lableObj.lable_ri = '';

            sss = doc.querySelector('.card-header h3');
            if (sss){
                lableObj.lable_zh = sss.textContent;
            }else {
                lableObj.lable_zh = '';
            }

            sss = queryElementBytext(doc, '.col-md-3 p', '作品时长');
            if (sss){
                lableObj.lable_shic = sss.textContent;
            }else {
                lableObj.lable_shic = '';
            }

            sss = doc.querySelectorAll('.col-12 .m-title.m-0 a');
            if (sss && sss.length > 0){
                let arr = Array.from(sss).map(obj => obj.textContent);
                let str = '';
                for (let i=0; i<arr.length; i++){
                    let item = arr[i];
                    if(i !== 0){
                        str += '、';
                    }
                    str += item;
                }
                lableObj.lable_yany = str;
            }else {
                lableObj.lable_yany = '';
            }

            sss = doc.querySelector('.card-body h5');
            if (sss){
                sss = sss.parentElement.querySelectorAll('p');
            }
            if (sss && sss.length > 0){
                lableObj.lable_jies = sss[1].textContent;
            }else {
                lableObj.lable_jies = '';
            }
            callInfo(lableObj);

            // 查询特定的元素
            const imglist = doc.querySelectorAll('.card-body .screenshot-link img');
            // 处理查询到的元素
            if (imglist && imglist.length > 0) {
                console.log('Found element:', imglist);
                // 你可以在这里对elements进行进一步处理，比如获取其属性或内容
                let imgArr = Array.from(imglist);
                imgArr.sort((a, b) => {
                    let anum = a.src.match(/-(\d{1,2}).jpg/)[1];
                    let bnum = b.src.match(/-(\d{1,2}).jpg/)[1];

                    return anum - bnum;
                });
                for (let i = 0; i < imgArr.length; i++) {
                    let img = imgArr[i];
                    imgAddrs = img.src.split('?');
                    callback(2, imgAddrs[0]);
                }
                return true;

            } else {
                throw new Error('Element not found');


            }
        }).then(res => {
        if (callFinish) {
            setTimeout(function () {
                callFinish(res);
            }, 500);
        }

    }).catch(error => {
        // 处理错误
        console.error('fetch error:', error);
        if (error.message === 'Network response was not ok') {
            searchFileList_javbus(javCode, cls, callback, callFinish, callInfo);

        } else if (error.message === 'Element not found') {
            searchFileList_javbus(javCode, cls, callback, callFinish,callInfo);

        } else {
            callback(-1, null);
        }
    });
}

function searchFileList_javbus(javCode, cls, callback, callFinish = null, callInfo = null) {
    // 请求URL
    // const url = 'http://localhost:10168/proxy?target_url=https://tor05.com/en/videos/' + javCode.toLowerCase();
    const url = 'http://localhost:10168/site/javbus/' + javCode.toLowerCase() + '.html';

    // 使用fetch API发送请求
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 将响应转换为文本（如果是HTML页面）
            return response.text();
        })
        .then(html => {
            // 创建一个DOM解析器
            const parser = new DOMParser();
            // 将HTML字符串解析为DOM文档
            const doc = parser.parseFromString(html, 'text/html');

            const lableObj = {};
            let sss = doc.querySelector('.col-md-3.info span[style]');
            if (sss){
                lableObj.lable_code = sss.textContent;
            }else {
                lableObj.lable_code = '';
            }

            sss = doc.querySelector('.container h3');
            if (sss){
                lableObj.lable_ri = sss.textContent;
            }else {
                lableObj.lable_ri = '';
            }

            lableObj.lable_zh = '';

            sss = queryElementBytext(doc, '.col-md-3.info p', '長度');
            if (sss){
                lableObj.lable_shic = sss.textContent;
            }else {
                lableObj.lable_shic = '';
            }

            sss = doc.querySelectorAll('.col-md-3.info span[onmouseover]');
            if (sss && sss.length > 0){
                let arr = Array.from(sss).map(obj => obj.textContent.replace(/[\r\n\t]+/g, ''));
                let str = '';
                for (let i=0; i<arr.length; i++){
                    let item = arr[i];
                    if(i !== 0){
                        str += '、';
                    }
                    str += item;
                }
                lableObj.lable_yany = str;
            }else {
                lableObj.lable_yany = '';
            }

            lableObj.lable_jies = '';

            callInfo(lableObj);

            // 查询特定的元素
            const imgAaalist = doc.querySelectorAll('#sample-waterfall a');
            // 处理查询到的元素
            if (imgAaalist && imgAaalist.length > 0) {
                console.log('Found element:', imgAaalist);
                // 你可以在这里对elements进行进一步处理，比如获取其属性或内容
                for (let i = 0; i < imgAaalist.length; i++) {
                    let aaa = imgAaalist[i];
                    callback(2, aaa.href);
                }
                return true;

            } else {
                throw new Error('Element not found');


            }
        }).then(res => {
        if (callFinish) {
            setTimeout(function () {
                callFinish(res);
            }, 500);
        }

    }).catch(error => {
        // 处理错误
        console.error('fetch error:', error);
        if (error.message === 'Network response was not ok') {
            searchFileList_tor05(javCode, cls, callback, callFinish, callInfo);

        } else if (error.message === 'Element not found') {
            searchFileList_tor05(javCode, cls, callback, callFinish, callInfo);

        } else {
            callback(-1, null);
        }
    });
}

function searchFileList_javdb(javCode, cls, callback, callFinish = null, callInfo = null, callCili=null) {
    // 请求URL
    // const url = 'http://localhost:10168/proxy?target_url=https://tor05.com/en/videos/' + javCode.toLowerCase();
    const url = 'http://localhost:10168/site/javdb_search/' + javCode.toLowerCase() + '.html';

    // 使用fetch API发送请求
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 将响应转换为文本（如果是HTML页面）
            return response.text();

        }).then(html => {
        const parser = new DOMParser();
        const doc = parser.parseFromString(html, 'text/html');
        const movieList = doc.getElementsByClassName('movie-list');
        if (movieList.length > 0) {
            const firstElement = movieList[0];
            const firstLink = firstElement.querySelector('a');
            if (firstLink) {
                const href = firstLink.getAttribute('href');
                const javdbCode = href.split('/').slice(-1)[0];
                const fullUrl = 'http://localhost:10168/site/javdb/' + javdbCode + '.html';

                return fetch(fullUrl);
            }
        }
        throw new Error('Moive not found');

    })
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 将响应转换为文本（如果是HTML页面）
            return response.text();

        })
        .then(html => {
            // 创建一个DOM解析器
            const parser = new DOMParser();
            // 将HTML字符串解析为DOM文档
            const doc = parser.parseFromString(html, 'text/html');

            const lableObj = {};
            let sss = doc.querySelector('.first-block span');
            if (sss){
                lableObj.lable_code = sss.textContent;
            }else {
                lableObj.lable_code = '';
            }

            sss = doc.querySelector('.origin-title');
            if (sss){
                lableObj.lable_ri = sss.textContent;
            }else {
                lableObj.lable_ri = '';
            }

            sss = doc.querySelector('.current-title');
            if (sss){
                lableObj.lable_zh = sss.textContent;
            }else {
                lableObj.lable_zh = '';
            }

            sss = queryElementBytext(doc, '.panel-block', '時長');
            if (sss){
                lableObj.lable_shic = sss.textContent;
            }else {
                lableObj.lable_shic = '';
            }

            sss = doc.querySelectorAll('.panel-block a[href^="/actors"]');
            if (sss && sss.length > 0){
                let arr = [];
                for (obj of sss) {
                    let nextElement = obj.nextElementSibling;
                    if (nextElement && nextElement.textContent === '♀'){
                        arr.push(obj.textContent);
                    }
                }
                let str = '';
                for (let i=0; i<arr.length; i++){
                    let item = arr[i];
                    if(i !== 0){
                        str += '、';
                    }
                    str += item;
                }
                lableObj.lable_yany = str;
            }else {
                lableObj.lable_yany = '';
            }

            lableObj.lable_jies = '';

            callInfo(lableObj);

            // 磁力元素
            const ciliEles = doc.querySelectorAll('.item.columns');
            if (ciliEles && ciliEles.length > 0){
                for (ciliEle of ciliEles){
                    let sizeSpan = ciliEle.querySelector('.meta');
                    if (sizeSpan){
                        sizeSpan.textContent = sizeSpan.textContent.replace('B', '').replace('M', 'm').replace(',', '      ------    ')
                    }

                    let tagsSpan = ciliEle.querySelector('.tags');
                    if (tagsSpan){
                        tagsSpan.textContent = '【【' + tagsSpan.textContent + '】】';
                    }

                    let btn = ciliEle.querySelector('.buttons.column button');
                    if (btn){
                        let magnetLink = btn.getAttribute('data-clipboard-text');
                        let imgHtml = '<img src="https://cdn.jsdelivr.net/gh/zxf10608/JavaScript/icon/115logo.ico" class="115offline" data-href='+magnetLink+' style="z-index:9123456789;display:inline-block;cursor:pointer;margin:0px 5px 2px;border-radius:50%;border:0px;vertical-align:middle;outline:none!important;padding:0px!important;height:20px!important;width:20px!important;left:0px!important;top:0px!important;" title="使用115网盘离线下载，右键复制地址\n'+magnetLink+'">';

                        let div = document.createElement('div');
                        div.className = "panel-footer";
                        div.innerHTML += imgHtml;
                        btn.parentElement.appendChild(div);
                    }
                    callCili(0, ciliEle.outerHTML);
                }
            }else {
                callCili(-1, null);
            }

            // 预览图元素
            const imgAaalist = doc.querySelectorAll('.tile-images.preview-images .tile-item');
            // 处理查询到的元素
            if (imgAaalist && imgAaalist.length > 0) {
                console.log('Found element:', imgAaalist);
                // 你可以在这里对elements进行进一步处理，比如获取其属性或内容
                for (let i = 0; i < imgAaalist.length; i++) {
                    let aaa = imgAaalist[i];
                    callback(2, aaa.href);
                }
                return true;

            } else {
                throw new Error('Element not found');
            }

        }).then(res => {
        if (callFinish) {
            setTimeout(function () {
                callFinish(res);
            }, 500);
        }

    }).catch(error => {
        // 处理错误
        console.error('fetch error:', error);
        if (error.message === 'Network response was not ok') {
            callback(-1, null);

        } else if (error.message === 'Element not found') {
            callback(-1, null);

        } else {
            callback(-1, null);
        }
    });
}

function searchFileFlag(avcode, callFlag){
    // 使用 fetch 发送 GET 请求
    const url = 'http://localhost:10168/flag?avcode=' + avcode
    fetch(url)
        .then(response => {
            // 检查响应是否成功
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            // 解析JSON
            return response.json();
        })
        .then(resultJson => {
            // 处理JSON数据
            callFlag(0, resultJson.flag)
        })
        .catch(error => {
            // 处理请求过程中出现的错误
            console.error('fetch error:', error);
            callFlag(-1, null)
        });

}

function queryElementBytext(doc, flag, searchText){
    let elements = doc.querySelectorAll(flag);
    let filteredElements = Array.from(elements).filter(el => el.textContent.includes(searchText));
    if (filteredElements[0]){
        return filteredElements[0];
    }

    return null;
}